package jsyf.oa.dao.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.report.ModuleEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Lenovo on 2017/11/24.
 */
@Repository
public class ReportModuleDao extends BaseDao {
    public ReportModuleDao() {
        super.iBaseEntity = new ModuleEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, ModuleEntity.class);
    }

    public List<JSONObject> getModuleENamesByUserId(String userId) {
        String sql = "SELECT\n" +
                "\tT_RPT_MODULE.ID AS id,T_RPT_MODULE.`NAME` AS name,T_RPT_MODULE.E_NAME AS eName,T_RPT_MODULE.APP_URL AS appUrl,T_RPT_MODULE.APP_TODAY_REPORT as appTodayReport\n" +
                "FROM\n" +
                "\tT_RPT_MODULE\n" +
                "WHERE\n" +
                "\tID IN (\n" +
                "\t\tSELECT\n" +
                "\t\t\tMODULE_ID\n" +
                "\t\tFROM\n" +
                "\t\t\tT_RPT_TEMPLATE_2_MODULE\n" +
                "\t\tWHERE\n" +
                "\t\t\tTEMPLATE_ID = (\n" +
                "\t\t\t\tSELECT\n" +
                "\t\t\t\t\tID\n" +
                "\t\t\t\tFROM\n" +
                "\t\t\t\t\tT_RPT_TEMPLATE\n" +
                "\t\t\t\tWHERE\n" +
                "\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\tREPORT_TEMPLATE_ID\n" +
                "\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\tT_SYS_ROLE_NAME\n" +
                "\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\t\t\tROLE_NAME_ID\n" +
                "\t\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\t\tT_SYS_ROLE\n" +
                "\t\t\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\t\t\t\t\tROLE_ID\n" +
                "\t\t\t\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\t\t\t\tT_SYS_USER\n" +
                "\t\t\t\t\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\t\t\t\t\tID = '" + userId + "'\n" +
                "\t\t\t\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t)\n" +
                "\t\t\t)\n" +
                "\t) OR ID IN (SELECT MODULE_ID FROM t_sys_user_2_report WHERE USER_ID='" + userId + "')\n " +
                "ORDER BY SORT_NO";
        List<JSONObject> list = super.queryForJsonList(sql);
        if (list.size() > 0) {
            return list;
        } else {
            String sqltemplate = "SELECT\n" +
                    "\tT_RPT_MODULE.ID AS id,\n" +
                    "\tT_RPT_MODULE.`NAME` AS NAME,\n" +
                    "\tT_RPT_MODULE.E_NAME AS eName" +
                    ",T_RPT_MODULE.APP_TODAY_REPORT as appTodayReport\n" +
                    "FROM\n" +
                    "\tT_RPT_MODULE\n" +
                    "WHERE\n" +
                    "\tID IN (\n" +
                    "\t\tSELECT\n" +
                    "\t\t\tMODULE_ID\n" +
                    "\t\tFROM\n" +
                    "\t\t\tT_RPT_TEMPLATE_2_MODULE\n" +
                    "\t\tWHERE\n" +
                    "\t\t\tT_RPT_TEMPLATE_2_MODULE.TEMPLATE_ID = '2017112614024602802C0A92AC550741'\n" +
                    "\t)";
            return super.queryForJsonList(sqltemplate.toString());
        }
    }
}
